package com.sinosoft.orderinfomodel;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.GenericDaoHibernate;
import com.sinosoft.util.DateTimeUtils;

@SuppressWarnings("unchecked")
@Repository("OrderInfoModelDao")
public class OrderInfoModelDaoHibernate extends
		GenericDaoHibernate<OrderInfoModel, Long> implements OrderInfoModelDao {

	public OrderInfoModelDaoHibernate() {
        super(OrderInfoModel.class);
    }

	public List<OrderInfoModel> getOrderInfoModelList(String hql){
		List<OrderInfoModel> modellist = getHibernateTemplate().find(hql.toString());
		return modellist;
	}
	
	
	public OrderInfoModel saveOrderInfoModel(OrderInfoModel o)
	{
		getHibernateTemplate().saveOrUpdate(o);
		return o;
	}
	
	/**
	 * 修改订单模板
	 * @param o
	 * @return
	 */
	public OrderInfoModel updateInfoModel(OrderInfoModel o)
	{
		getHibernateTemplate().merge(o);
		return o;
	}
	
	/**
	 * 根据id查询订单模版对象
	 * @param id 订单id
	 * @return
	 */
	public OrderInfoModel getOrderInfoModelById(String id)
	{
		OrderInfoModel o = new OrderInfoModel();
		String hql = "from OrderInfoModel o where o.ordermodelid="+id;
		List<OrderInfoModel> orderlist = getHibernateTemplate().find(hql);
		if(orderlist != null)
			o = orderlist.get(0);
		return o;
	}

	/**
	 * 删除订单模板
	 */
	public void removeOrderInfo(OrderInfoModel orderinfo) {
		getHibernateTemplate().delete(orderinfo);
	}
	
	//根据用户名和是否默认模版查询订单模版
	public OrderInfoModel getOrderInfoModelByDefaultAndUsername(String username,int isdefault)
	{
		String hqls = "from OrderInfoModel where userid='"+username+"' and isdefaulmodel="+isdefault;
		List<OrderInfoModel> omlist = getHibernateTemplate().find(hqls);
		OrderInfoModel oimodel = null;
		if(omlist != null && omlist.size()>0)
			oimodel = omlist.get(0);
		return oimodel;
	}
	
	public OrderInfoModel getOrderInfoModelBySystem()
	{
		String hqls = "from OrderInfoModel where isdefaulmodel=3";
		List<OrderInfoModel> omlist = getHibernateTemplate().find(hqls);
		OrderInfoModel oimodel = null;
		if(omlist != null && omlist.size()>0)
			oimodel = omlist.get(0);
		return oimodel;
	}
	
	/**
	 * 获取系统默认模版
	 * @return
	 */
	public OrderInfoModel getSystemOrderInfoModel()
	{
		String hqls = "from OrderInfoModel where isdefaulmodel=3";
		List<OrderInfoModel> oimlist = getHibernateTemplate().find(hqls);
		if(oimlist!=null && oimlist.size()>0)
			return oimlist.get(0);
		else
			return null;
	}
	
	public int getOrderInfoModelCount(String sql)
	{
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createSQLQuery(sql);
		int count = Integer.parseInt(query.list().get(0).toString());
		session.close();
		return count;
	}
	
	public List<OrderInfoModel> getOrderInfoModelList(OrderInfoModel orderinfo,int first,int pagesize)
	{
		Session session =null;
		List<OrderInfoModel> orderinfolist = null;
		session=this.getHibernateTemplate().getSessionFactory().openSession();
		Criteria c = session.createCriteria(OrderInfoModel.class);
		c.setFirstResult(first);
		c.setMaxResults(pagesize);
		c.addOrder(Order.desc("productlevel"));
		c.addOrder(Order.desc("satelliteid"));
		c.addOrder(Order.desc("sensorid"));
		BigDecimal id = orderinfo.getOrdermodelid();
		BigDecimal priority = orderinfo.getPriority();
		String satelliteid = orderinfo.getSatelliteid();
		String userid = orderinfo.getUserid();
		String sensorid = orderinfo.getSensorid();
		String starttime = orderinfo.getStarttime();
		String endtime = orderinfo.getEndtime();
		if(id!=null&&!id.equals(""))
		{
			c.add(Restrictions.eq("ordermodelid", id));
		}
		if(priority!=null&&!priority.equals(""))
		{
			c.add(Restrictions.eq("priority", priority));
		}
		if(satelliteid!=null&&!satelliteid.isEmpty())
		{
			c.add(Restrictions.eq("satelliteid", satelliteid));
		}
		if(userid!=null&&!userid.isEmpty())
		{
			c.add(Restrictions.like("userid", "%"+userid+"%"));
		}
		if(sensorid != null && !sensorid.isEmpty())
		{
			c.add(Restrictions.eq("sensorid", sensorid));
		}
		if(starttime != null && !starttime.isEmpty())
		{
			c.add(Restrictions.gt("createtime", DateTimeUtils.StringToDate(starttime,"yyyy-MM-dd")));
		}
		if(endtime != null && !endtime.isEmpty())
		{
			c.add(Restrictions.lt("createtime", DateTimeUtils.StringToDate(endtime,"yyyy-MM-dd")));
		}
		orderinfolist = c.list();
		session.close();
		return orderinfolist;
	}
	
	/**
	 * 查询订单模板
	 * @param sql
	 * @return
	 */
	public List<OrderInfoModel> getOrderInfoModel(String sql)
	{
		List<OrderInfoModel> ordermodellist = getHibernateTemplate().find(sql);
		return ordermodellist;
	}
	
	/**
	 * 通过产品级别，卫星，传感器查询订单模板
	 */
	public OrderInfoModel getSystemOrderInfoModel(String productlevel,String satelliteid,String sensorid)
	{
		StringBuffer hql = new StringBuffer("from OrderInfoModel where productlevel='");
		hql.append(productlevel+"'");
		hql.append(" and satelliteid='"+satelliteid+"'");
		hql.append(" and sensorid='"+sensorid+"'");
		List<OrderInfoModel> oimodellist = getHibernateTemplate().find(hql.toString());
		if(oimodellist!=null && oimodellist.size()>0)
			return oimodellist.get(0);
		else
			return null;
	}
	
	public OrderInfoModel saveOrderInfoModelExistDefaultModel(OrderInfoModel o,String username, int defa)
	{
		String hqls = "from OrderInfoModel where userid='"+username+"' and isdefaulmodel=2";
		List<OrderInfoModel> omlist = this.getHibernateTemplate().find(hqls);
		for (int i = 0; i < omlist.size(); i++) {
			OrderInfoModel om = omlist.get(i);
			om.setIsdefaulmodel(new BigDecimal(2));
			getHibernateTemplate().merge(om);
		}
		return getHibernateTemplate().merge(o);
	}
}
